package com.tqjc.system.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tqjc.system.common.controller.BaseController;
import com.tqjc.system.common.entity.BO.SysMaterialInfoBO;
import com.tqjc.system.common.entity.VO.AreaInfoVO;
import com.tqjc.system.common.entity.VO.FatherAreaInfoVO;
import com.tqjc.system.common.exception.AddTaskException;
import com.tqjc.system.core.model.AjaxJson;
import com.tqjc.system.core.model.PageInfo;
import com.tqjc.system.core.util.ResponseUtil;
import com.tqjc.system.system.entity.SysAreaInfoEntity;
import com.tqjc.system.system.entity.SysMaterialInfoEntity;
import com.tqjc.system.system.service.SysAreaInfoService;
import com.tqjc.system.system.wrapper.AreaManageWrapper;
import com.tqjc.system.system.wrapper.MaterialManageWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.IdentityHashMap;
import java.util.List;

/**
 * @description:
 * @Author: 86195
 * @Date: 2023/12/26 9:20
 **/
@Controller
@RequestMapping("/sys/area")
@Slf4j
public class SysAreaManageController extends BaseController {

    @Autowired
    private SysAreaInfoService areaInfoService;


    @RequestMapping("/index")
    public String index(Model model){
        return "sys/manage/area_index.html";
    }


    @RequestMapping("/openEditPage")
    public String openEditPage(Model model){
        return "sys/manage/areaDetail.html";
    }



    @RequestMapping("/infoList")
    public void infoList(AreaInfoVO areaInfoVO, HttpServletResponse response){
        try {

            log.info("查询库区信息接口开始");
            List<SysAreaInfoEntity> lstResult = areaInfoService.getList(areaInfoVO);
            AreaManageWrapper areaManageWrapper = new AreaManageWrapper(lstResult);
            ResponseUtil.write(response, areaManageWrapper.wrap());
            log.info("查询库区信息接口结束");
        } catch (Exception e){
            log.error("查询库区信息接口运行异常",e);
        }

    }



    @RequestMapping("/query/id")
    public String toEditPage(String id,Model model){
        log.info("主键查询库区接口开始");
        SysAreaInfoEntity sysAreaInfoEntity = areaInfoService.getById(id);
        model.addAttribute("sysAreaInfoEntity",sysAreaInfoEntity);
        log.info("主键查询库区接口结束");
        return "sys/manage/areaDetail.html";

    }


    @RequestMapping("/update")
    @ResponseBody
    public AjaxJson updateInfo(AreaInfoVO areaInfoVO){
        AjaxJson ajaxJson = new AjaxJson();
        try {

            log.info("库区信息更新接口开始");

            areaInfoService.updateInfo(areaInfoVO);
            log.info("库区信息更新接口结束");
        } catch (Exception e) {
            log.error("库区信息更新接口错误",e);
            ajaxJson.setMsg("库区信息更新异常");
            ajaxJson.setSuccess(false);
        }
        return ajaxJson;

    }

    @RequestMapping("/openAdd")
    public String openAdd(String idKey, Model model){

        try {
            log.info("访问一级库区信息接口开始");
            FatherAreaInfoVO fatherAreaInfoVO =  areaInfoService.getFatherAreaInfo(idKey);
            model.addAttribute("fatherAreaInfoVO",fatherAreaInfoVO);
            log.info("访问一级库区信息接口结束");


        } catch (Exception e){
            log.error("新增子区域接口失败",e);

        }

        return "sys/manage/addArea.html";

    }


    @RequestMapping("/addNum")
    @ResponseBody
    public AjaxJson addAreaNum(FatherAreaInfoVO fatherAreaInfoVO){
        AjaxJson ajaxJson = new AjaxJson();
        try {
            log.info("保存新增子区域数量接口开始");
            boolean flag = areaInfoService.addAreaNum(fatherAreaInfoVO);
            String msg = flag ? "新增成功" : "新增失败";
            ajaxJson.setMsg(msg);
            ajaxJson.setSuccess(flag);
            log.info("保存新增子区域数量接口结束");

        } catch (Exception e){
            log.error("新增子区域接口失败",e);
            ajaxJson.setMsg("新增失败,请联系管理员");
            ajaxJson.setSuccess(false);
        }

        return ajaxJson;

    }


    @RequestMapping("/saveChildArea")
    @ResponseBody
    public AjaxJson saveChildArea(AreaInfoVO areaInfoVO){
        AjaxJson ajaxJson = new AjaxJson();
        try {
            log.info("保存修改子区域接口开始");
            boolean flag = areaInfoService.addChildArea(areaInfoVO);
            String msg = flag ? "修改成功" : "修改失败";
            ajaxJson.setMsg(msg);
            ajaxJson.setSuccess(flag);
            log.info("保存修改子区域接口结束");

        } catch (AddTaskException e){
            log.error("保存修改子区域接口失败",e);
            ajaxJson.setMsg(e.getMessage());
            ajaxJson.setSuccess(false);
        } catch (Exception e){
            log.error("保存修改子区域接口失败",e);
            ajaxJson.setMsg("修改失败,请联系管理员");
            ajaxJson.setSuccess(false);
        }

        return ajaxJson;

    }


    @RequestMapping("/delChildArea")
    @ResponseBody
    public AjaxJson delChildArea(String idKey){
        AjaxJson ajaxJson = new AjaxJson();
        try {
            log.info("删除子区域接口开始");
            boolean flag = areaInfoService.delChildArea(idKey);
            String msg = flag ? "删除成功" : "删除失败";
            ajaxJson.setMsg(msg);
            ajaxJson.setSuccess(flag);
            log.info("删除子区域接口结束");

        } catch (Exception e){
            log.error("删除子区域接口失败",e);
            ajaxJson.setMsg("删除失败,请联系管理员");
            ajaxJson.setSuccess(false);
        }

        return ajaxJson;

    }




}
